home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch9 / SpTriang.cls < prev    next >
Encoding:
Visual Basic class definition  |  1999-05-28  |  2.9 KB  |  105 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "TriangleSprite"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = False
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. ' Moving triangle sprite.
  15. Option Explicit
  16.  
  17. Implements Sprite
  18.  
  19. ' The three corners are stored as the distance
  20. ' PtR(i) and angle PtT(i) from the center of the
  21. ' triangle (Cx, Cy) to the corner. This makes it
  22. ' easier to rotate the triangle. When rotated by
  23. ' angle Theta, the coordinates of corner i are:
  24. '
  25. '   x = Cx + PtR(i) * Cos(PtT(i) + Theta)
  26. '   y = Cy + PtR(i) * Sin(PtT(i) + Theta)
  27. '
  28. Private PtR(1 To 3) As Integer
  29. Private PtT(1 To 3) As Single
  30. Private Cx As Integer       ' Position of center.
  31. Private Cy As Integer
  32. Private Vx As Integer       ' Velocity.
  33. Private Vy As Integer
  34. Private Theta As Single     ' Orientation.
  35. Private Vtheta As Single    ' Angular velocity.
  36. Private Color As Long
  37.  
  38. Private Type POINTAPI
  39.     x As Long
  40.     y As Long
  41. End Type
  42. Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
  43.  
  44. ' Draw the triangle on the indicated picture box.
  45. Public Sub Sprite_DrawSprite(ByVal pic As PictureBox)
  46. Dim i As Integer
  47. Dim pts(1 To 3) As POINTAPI
  48.  
  49.     ' Compute the current corner locations.
  50.     For i = 1 To 3
  51.         pts(i).x = Cx + PtR(i) * Cos(PtT(i) + Theta)
  52.         pts(i).y = Cy + PtR(i) * Sin(PtT(i) + Theta)
  53.     Next i
  54.  
  55.     ' Draw the triangle.
  56.     pic.FillColor = Color
  57.     pic.ForeColor = Color
  58.     Polygon pic.hdc, pts(1), 3
  59. End Sub
  60.  
  61. ' Initialize the rectangle.
  62. Public Sub InitializeTriangle(ByVal new_cx As Integer, ByVal new_cy As Integer, ByVal new_vx As Integer, ByVal new_vy As Integer, ByVal r1 As Integer, ByVal t1 As Integer, ByVal r2 As Integer, ByVal t2 As Integer, ByVal r3 As Integer, ByVal t3 As Integer, ByVal new_theta As Single, ByVal new_vtheta As Single, ByVal new_color As Long)
  63.     Cx = new_cx
  64.     Cy = new_cy
  65.     Vx = new_vx
  66.     Vy = new_vy
  67.  
  68.     PtR(1) = r1
  69.     PtT(1) = t1
  70.     PtR(2) = r2
  71.     PtT(2) = t2
  72.     PtR(3) = r3
  73.     PtT(3) = t3
  74.  
  75.     Theta = new_theta
  76.     Vtheta = new_vtheta
  77.     Color = new_color
  78. End Sub
  79. ' Add the velocity components to the sprite's
  80. ' position components.
  81. Public Sub Sprite_MoveSprite(ByVal xmin As Integer, ByVal xmax As Integer, ByVal ymin As Integer, ByVal ymax As Integer)
  82.     Cx = Cx + Vx
  83.     Cy = Cy + Vy
  84.  
  85.     ' Keep the object within the drawing area.
  86.     If (Cx < xmin) Then
  87.         Cx = 2 * xmin - Cx
  88.         Vx = -Vx
  89.     ElseIf (Cx > xmax) Then
  90.         Cx = 2 * xmax - Cx
  91.         Vx = -Vx
  92.     End If
  93.     If (Cy < ymin) Then
  94.         Cy = 2 * ymin - Cy
  95.         Vy = -Vy
  96.     ElseIf (Cy > ymax) Then
  97.         Cy = 2 * ymax - Cy
  98.         Vy = -Vy
  99.     End If
  100.  
  101.     Theta = Theta + Vtheta
  102. End Sub
  103.  
  104.  
  105.